import Vue from "vue";
import FeatureOverlay from "@/components/FeatureOverlay.vue";

let FeatureOverlayConstructor = Vue.extend(FeatureOverlay);

let lastInstance = null;

/**
 * 显示Overlay
 * 
 * @param {object} options map: 地图对象, feature: 关联的要素, fit: 是否移动到地图中心, editing: 该要素是否是编辑状态
 */
const show = function (options) {
    if (lastInstance) {
        lastInstance.closePopup();
        lastInstance = null;
    }
    lastInstance = new FeatureOverlayConstructor({
        data: options
    });
    lastInstance.$mount();
    document.body.appendChild(lastInstance.$el);
    lastInstance.showPopup();
};

const closeAll = function () {
    if (lastInstance) {
        lastInstance.closePopup();
    }
};

let overlay = {
    show,
    closeAll
};

let OverlayPlugin = {
    install: function (_Vue) {
        _Vue.prototype.$overlay = overlay;
    }
};

Vue.use(OverlayPlugin);